﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ClientAppViaWebService.PersonsLogicServiceRef;

namespace ClientAppViaWebService.Forms
{
    public partial class PersonEditForm : TObjectEditForm
    {
        private PersonsLogicService lService;
        private readonly Int32 ClassID = 16;

        public PersonEditForm()
        {
            InitializeComponent();
            lService = new PersonsLogicService();
            classTextBox.Text = ClassID.ToString();
        }

        public String FirstName
        {
            get { return firstNameTextBox.Text; }
            set { firstNameTextBox.Text = value; }
        }

        public String LastName
        {
            get { return lastNameTextBox.Text; }
            set { lastNameTextBox.Text = value; }
        }

        public String Address
        {
            get { return addressTextBox.Text; }
            set { addressTextBox.Text = value; }
        }

        public DateTime BirthDate
        {
            get { return birthDateTimePicker.Value; }
            set { birthDateTimePicker.Value = value; }
        }
        
        protected override void SetFormProperties(Object _object)
        {
            Person d = (_object as Person);
            ID = d.ID;
            EXT_ID = d.EXT_ID;
            ParentId = d.PARENT_ID;
            Class = d.CLASS_ID;
            ObjName = d.NAME;
            FirstName = d.FIRST_NAME;
            LastName = d.LAST_NAME;
            Address = d.ADDRESS;
            BirthDate = d.BIRTH_DATE;
        }

        protected override Object RetrieveFormProperties()
        {
            Person person = new Person();
            person.ID = ID;
            person.EXT_ID = EXT_ID;
            person.PARENT_ID = ParentId;
            person.CLASS_ID = Class;
            person.NAME = ObjName;
            person.FIRST_NAME = FirstName;
            person.DataBaseName = DataBaseName;
            person.LAST_NAME = LastName;
            person.ADDRESS = Address;
            person.BIRTH_DATE = BirthDate;
            return person;
        }

        protected override void LoadEditFormData(int parentNodeID)
        {
            Person p = lService.SelectPerson_By_DataBase_And_Id(DataBaseName, parentNodeID);
            SetFormProperties(p);
        }

        protected override void UpdateEntity()
        {
            Person obj = RetrieveFormProperties() as Person;
            lService.UpdatePerson(obj);
        }

        protected override void InsertEntity()
        {
            Person obj = RetrieveFormProperties() as Person;
            lService.InsertPerson(obj);
        }
    }
}
